package com.hdax.dm.mapstructs;

import com.hdax.dm.dtos.item.ItemDescDto;
import com.hdax.dm.entity.base.DmImage;
import com.hdax.dm.entity.item.DmCinema;
import com.hdax.dm.entity.item.DmItem;
import com.hdax.dm.feign.item.BaseFeign;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Mapper(componentModel = "spring")
public abstract class ItemDescDtoMapper {


    @Autowired
    private BaseFeign baseFeign;

    @Mappings({
            @Mapping(target = "address",expression = "java(getAddress(source.getCinema()))"),
            @Mapping(target = "areaId",expression = "java(getAreaId(source.getCinema()))"),
            @Mapping(target = "areaName",expression = "java(getAreaName(source.getCinema()))"),
            @Mapping(target = "imgUrl",expression = "java(getImgUrl(source.getId()))")
    })
    public abstract ItemDescDto toTarget(DmItem source);
    public abstract List<ItemDescDto> toTargetList(List<DmItem> sources);

    protected String getAddress(DmCinema cinema){
        return cinema.getAddress() + " - " + cinema.getAreaName();
    }

    protected Long getAreaId(DmCinema cinema){
        return cinema.getAreaId();
    }

    protected String getAreaName(DmCinema cinema){
        return cinema.getAreaName();
    }

    protected String getImgUrl(Long id){
        DmImage image = baseFeign.image(id);
        return image == null ? "":image.getImgUrl();
    }
}
